﻿@using System.Text
@model ShipmentModel
@{
    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Shipments.AddNew.Title").Text, Model.OrderId);

    var itemsFromMultipleWarehouses = Model.Items
        .Where(x => x.AllowToChooseWarehouse && x.AvailableWarehouses.Count > 0)
        .ToList();
}
<form asp-area="@Constants.AreaAdmin" asp-controller="Shipment" asp-action="AddShipment" method="post"
      asp-route-orderId="@Context.Request.Query["orderId"]">
    
    <div asp-validation-summary="All"></div>

    <div class="col-md-12">
        <div class="x_panel light form-fit">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-truck"></i>
                    @string.Format(T("Admin.Orders.Shipments.AddNew.Title").Text, Model.OrderNumber)
                    <small>
                        <i class="fa fa-arrow-circle-left"></i>
                        @Html.ActionLink(T("Admin.Orders.Shipments.BackToOrder").Text, "Edit", "Order", new { Id = Model.OrderId })
                    </small>
                </div>
                <div class="actions">
                    <div class="btn-group btn-group-devided">
                        <button class="btn btn-success" type="submit" id="shipment-save" name="save"><i class="fa fa-check"></i> @T("Admin.Common.Save") </button>
                        <button class="btn btn-success" type="submit" id="shipment-save-continue" name="save-continue"><i class="fa fa-check-circle"></i> @T("Admin.Common.SaveContinue") </button>
                        <vc:admin-widget widget-zone="order_shipment_add_buttons" additional-data="null" />
                    </div>
                </div>
            </div>
            <div class="x_content form">
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="form-group">
                            <admin-label asp-for="TrackingNumber" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="TrackingNumber" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="AdminComment" />
                            <div class="col-md-9 col-sm-9">
                                <admin-input asp-for="AdminComment" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    @if (itemsFromMultipleWarehouses.Any())
    {
    <script>
            $(document).ready(function () {
                $('#shipment-save').click(function () {
                    return validateWarehouseAvailability();
                });
                $('#shipment-save-continue').click(function () {
                    return validateWarehouseAvailability();
                });
            });


            function validateWarehouseAvailability() {
                @{
                    var sb = new StringBuilder();
                    for (int i = 0; i <= itemsFromMultipleWarehouses.Count - 1; i++)
                    {
                        var item = itemsFromMultipleWarehouses[i];
                        sb.AppendFormat("validateAvailableQuantity('{0}')", item.OrderItemId);
                        if (i != itemsFromMultipleWarehouses.Count - 1)
                        {
                            sb.Append(" && ");
                        }
                    }
                }
                var valid = @(Html.Raw(sb.ToString()));
                if (!valid) {
                    return confirm('@T("Admin.Orders.Shipments.Products.Warehouse.QuantityNotEnough")');
                }
                return true;
            }

            function validateAvailableQuantity(orderItemId) {
                var enteredValue = parseInt($('#qtyToAdd' + orderItemId).val(), 10);
                if (enteredValue <= 0)
                    return true;
                var reservedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('reserved-qty'), 10);
                var plannedValue = parseInt($('#warehouse_' + orderItemId).find(':selected').data('planned-qty'), 10);
                var availableToAdd = reservedValue - plannedValue;
                return enteredValue <= availableToAdd;
            }
    </script>
    }

                <h4><strong>@T("Admin.Orders.Shipments.Products")</strong></h4>

                <div class="table-responsive visible-xs-block visible-sm-block visible-md-block visible-lg-block">
                    <table class="table table-bordered table-striped table-hover">
                        <thead>
                            <tr>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.ProductName")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.SKU")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.Warehouse")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.ItemWeight")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.ItemDimensions")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.QtyOrdered")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.QtyShipped")
                                </th>
                                <th>
                                    @T("Admin.Orders.Shipments.Products.QtyToShip")
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.Items)
                            {
                                <tr>
                                    <td>
                                        <div style="padding-left:10px;padding-right:10px;">
                                            <em><a href="@Url.Action("Edit", "Product", new {id = item.ProductId})">@item.ProductName</a></em>
                                            @if (!String.IsNullOrEmpty(item.AttributeInfo))
                                            {
                                                <p>
                                                    @Html.Raw(item.AttributeInfo)
                                                </p>
                                            }
                                            @if (item.ShipSeparately)
                                            {
                                                <p>
                                                    <strong>@T("Admin.Orders.Shipments.Products.ShipSeparately.Warning")</strong>
                                                    @T("Admin.Orders.Shipments.Products.ShipSeparately")
                                                </p>
                                            }
                                        </div>
                                    </td>
                                    <td>
                                        <div style="padding-left:10px;padding-right:10px;">
                                            @item.Sku
                                        </div>
                                    </td>
                                    <td>
                                        <div>
                                            @if (item.AllowToChooseWarehouse)
                                            {
                                                if (item.AvailableWarehouses.Count > 0)
                                                {
                                            <select name="warehouse_@(item.OrderItemId)" id="warehouse_@(item.OrderItemId)">
                                                @foreach (var warehouse in item.AvailableWarehouses)
                                                {
                                                    var selected = warehouse.WarehouseId == item.WarehouseId ? true : false;
                                                    <option value="@(warehouse.WarehouseId)" data-stock-qty="@(warehouse.StockQuantity)"
                                                            data-reserved-qty="@(warehouse.ReservedQuantity)"
                                                            data-planned-qty="@(warehouse.PlannedQuantity)"
                                                            selected="@selected">
                                                        @T("Admin.Orders.Shipments.Products.Warehouse.ChooseQty", warehouse.WarehouseName, warehouse.StockQuantity, warehouse.ReservedQuantity, warehouse.PlannedQuantity)
                                                    </option>
                                                }
                                            </select>
                                                }
                                                else
                                                {
                                                    <div>@T("Admin.Orders.Shipments.Products.Warehouse.NotAvailabe")</div>
                                                }
                                            }
                                            else
                                            {
                                                //display warehouses
                                                for (int i = 0; i < item.AvailableWarehouses.Count; i++)
                                                {
                                                    var warehouse = item.AvailableWarehouses[i];
                                                    @warehouse.WarehouseName
                                                    if (i != item.AvailableWarehouses.Count - 1)
                                                    {
                                                        <br />
                                                    }
                                                }
                                            }
                                        </div>
                                    </td>
                                    <td>
                                        @item.ItemWeight
                                    </td>
                                    <td>
                                        @item.ItemDimensions
                                    </td>
                                    <td>
                                        @item.QuantityOrdered
                                    </td>
                                    <td>
                                        @item.QuantityInAllShipments
                                    </td>
                                    <td>
                                        <input name="qtyToAdd@(item.OrderItemId)" type="text" value="@item.QuantityToAdd" id="qtyToAdd@(item.OrderItemId)" class="form-control" style="width:100px;" />
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
               

</form>
